var attendanceType;
var id;
var taskDetailsListTijaio;
var taskDetailsList;
$(function() {

	attendanceType = ly.getUrlParam("attendanceType");
	id = ly.getUrlParam("id");
	if(attendanceType == 2) {
		$("#title").html("查询");
		$(".form").getForm({
			'ajaxform': true,
			id: id
		}, function(data) {
			changeIsSpecify(data.isSpecify, data.businessId);
			$("#save-btn-commodityClassification").hide();
			if(data.taskDetailsList != null && data.taskDetailsList != '') {
				taskDetailsList = data.taskDetailsList;
				writeTaskDetailsList(taskDetailsList);
			}
			//selectUsersList(data.userId,"","","1");
			$("#taskType").getSelect({
				type: "radio",
				asName: "taskType",
				code: "taskType",
				default: data.taskType
			});
			$("#taskLevel").getSelect({
				type: "radio",
				asName: "taskLevel",
				code: "taskLevel",
				default: data.taskLevel
			});
			$("#taskCategories").getSelect({
				type: "radio",
				asName: "taskCategories",
				code: "taskCategories",
				default: data.taskCategories
			});
			$("#isSpecify").getSelect({
				type: "radio",
				asName: "isSpecify",
				list: [{
						value: "1",
						display: "指定人员"
					},
					{
						value: "2",
						display: "指定部门"
					},
					{
						value: "3",
						display: "指定工种"
					}
				],
				default: data.isSpecify
			});
		});
		$(".addDetailsList").remove();
		$(".radio").attr("disabled", "disabled");
	}

});

function changeIsSpecify(isSpecify, businessId) {

	if(isSpecify == '1') {
		//指定人员
		$(".changeWork").hide();
		$(".changeTeam").hide();
		$(".changeUserId").show();
		selectUsersList(businessId);
	} else if(isSpecify == '2') {
		//指定部门
		$(".changeWork").hide();
		$(".changeUserId").hide();
		$(".changeTeam").show();
		selectTeamList(businessId);
	} else {
		//指定工种
		$(".changeTeam").hide();
		$(".changeUserId").hide();
		$(".changeWork").show();
		selectWorkerList(businessId);
	}
}


//选择上级部门
function selectTeamList(businessId) {
	var list = new Array();
	request({
		url: site.gateUrl + "/back/team/selectTeamList",
		data: {
			'num': 1,
			'size': 100,
			'isUsing': 1
		},
		type: "get",
		ajaxform: true,
		callback: function(f) {
			console.log(f);
			if(f.code == "rest.success") {
				/*var obj = {value:' ',display:'请选择'};
				list.push(obj);*/
				var obj = {};
				if(f.result.list != null && f.result.list != '') {
					obj = {};
					$.each(f.result.list, function(i, o) {
						obj = {
							value: o.id,
							display: o.teamName
						};
						list.push(obj);
					});
				}
				$("#teamId").getSelect({
					type: "checkbox",
					asName: "teamId",
					list: list,
					default: businessId
				});
			}
		}
	});
}

//选择设备
function selectEquipmentListBack(equipmentType, equipmentName, taskDetailsList) {

	var changeThisList = "";
	var listEquipmentId = "";
	$("#changeThisList").empty();
	if(taskDetailsList != null && taskDetailsList != '') {
		$.each(taskDetailsList, function(i, o) {
			changeThisList = "<tr class='deleteThisEquipment" + o.id + "'><input class='id' type='hidden' value='" + o.id + "'><td>" + (i + 1) + "</td><td>" + o.equipmentName + "</td><td onclick=\"deleteThisEquipment('" + o.id + "')\">删除</td></tr>";
			listEquipmentId += o.id + ",";
		});
		$("#changeThisList").append(changeThisList);
	}
	request({
		url: site.gateUrl + "/back/attendanceBack/selectEquipmentListBack",
		data: {
			'num': 1,
			'size': 100,
			'isUsing': 1,
			'equipmentType': equipmentType,
			'equipmentName': equipmentName
		},
		type: "get",
		"ajaxform": true,
		callback: function(f) {
			console.log(f);
			if(f.code == "rest.success") {
				var equipmentList = null;
				if(f.result.list != null && f.result.list != '') {
					$("#equipmentList").empty();
					obj = {};
					$.each(f.result.list, function(i, o) {
						var c = "";
						if(listEquipmentId != null && listEquipmentId != '') {
							if(o.id.indexOf(listEquipmentId) == -1) {
								c = "已选择";
							} else {
								c = "选择";
							}
						} else {
							c = "选择";
						}
						equipmentList += "<tr><td>" + o.equipmentName + "</td>" +
							"<td>" + o.equipmentTypeDisplay + "</td>" +
							"<td>" + o.lat + "</td><td>" + o.log + "</td><td>" + o.equipmentAddress + "</td>" +
							"<td class='equipment" + o.id + "' onclick=\"changeThisEquipment('" + o.id + "','" + o.equipmentName + "')\">" + c + "</td>";
					});
				}
				$("#equipmentList").append(equipmentList);
			}
		}
	});
}

//选择工种
function selectWorkerList(businessId) {
	var list = new Array();
	request({
		url: site.gateUrl + "/back/worker/selectWorkerList",
		data: {
			'num': 1,
			'size': 100,
			'isUsing': 1
		},
		type: "get",
		ajaxform: true,
		callback: function(f) {
			console.log(f);
			if(f.code == "rest.success") {
				var obj = {};
				if(f.result.list != null && f.result.list != '') {
					obj = {};
					$.each(f.result.list, function(i, o) {
						obj = {
							value: o.id,
							display: o.workerName
						};
						list.push(obj);
					});
				}
				$("#workerId").getSelect({
					type: "checkbox",
					asName: "workerId",
					list: list,
					default: businessId
				});
			}
		}
	});
}

//选择用户
function selectUsersList(businessId) {
	var list = new Array();

	var teamId = "";
	var userType = "";
	var assumeOffice = "";
	var backstage_access_personnelstyle = window.localStorage.getItem("backstage-access-personnelstyle");
	if(backstage_access_personnelstyle == 1) {
		assumeOffice = "1";
	} else {
		assumeOffice = "2";
		if(window.localStorage.getItem("backstage-access-teamId") == 'undefined'){
			teamId = '';
		}else{
			teamId = window.localStorage.getItem("backstage-access-teamId");
		}
		userType = window.localStorage.getItem("backstage_access_personnelstyle");
	}
	request({
		url: site.gateUrl + "/sUsers/selectUsersList",
		data: {
			'num': 1,
			'size': 1000,
			'isUsing': 1,
			'userType': userType,
			'teamId': teamId,
			'assumeOffice': assumeOffice
		},
		type: "get",
		"ajaxform": true,
		callback: function(f) {
			console.log(f);
			if(f.code == "rest.success") {
				var obj = {};
				if(f.result.list != null && f.result.list != '') {
					obj = {};
					$.each(f.result.list, function(i, o) {
						obj = {
							value: o.id,
							display: o.nickName
						};
						list.push(obj);
					});
				}
				$("#userId").getSelect({
					type: "checkbox",
					asName: "userId",
					list: list,
					default: businessId
				});
			}
		}
	});

}




function writeTaskDetailsList(taskDetailsList) {

	var detailsList = "";
	$.each(taskDetailsList, function(i, o) {
		detailsList += "<tr><td>" + o.taskFlow + "</td>" +
			"<td>" + o.taskLat + "</td><td>" + o.taskLog + "</td><td>" + o.taskOperationDisplay + "</td>" +
			"<td>" + o.detailsExplain + "</td></tr>";
	});
	$("#detailsList").append(detailsList);
}